<?php

namespace app\api\controller\v2;

use app\admin\model\cms\ChartTableModel;
use app\admin\model\cms\Diyform;
use app\common\controller\Api;
use think\Db;
use function Sodium\add;

/**
 * 图表相关接口
 */
class Chart extends Api
{
    //无需登录的方法,同时也就不需要鉴权了
    protected $noNeedLogin = [''];
    //无需鉴权的方法,但需要登录
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    //获取首页顶部图表列表
    public function getTopList()
    {
        $request = $this->request->param();
        $user = $this->auth->getUser();//用户信息

        if (isset($request['startTime']) && isset($request['endTime'])) {
            $start_data = $request['start_data'];//开始时间 格式:Y-m-d
            $end_data = $request['end_data'];//结束时间 格式:Y-m-d
        } else {
            //默认最近一年
            $start_data = '';//循环是改为图表Diyform创建的时间减去1年的时间
            $end_data = '';//循环是改为图表Diyform创建的时间
        }

        //图表(量化信号)-详情表 回去权重>0的 为了得到线段
        $model = Diyform::where('status', 'normal')->order('weigh', 'asc')->limit(3)->select();

        $res = [];
        //循环需要的图表
        foreach ($model as $k => $v) {
            $fields = [];
            foreach ($v['fields'] as $fsv) {
                $fields = array_merge($fields, json_decode(trim($fsv, '[]'), true));
            }
            $field = [];
            foreach ($fields as $fields_k => $fields_v) {
                $field[] = $fields_k;
            }
            $field = array_merge($field, ['datetime']);

            //要是没有传递具体的时间则用fa_pro_diyform数据表常见的时间作为取值范围
            if (!$start_data && !$end_data){
                $create_time = strtotime($v['createtime']);
                $end_data = date('Y-m-d', $create_time);
                $one_year_ago_timestamp = strtotime($end_data) - 365 * 24 * 60 * 60;
                $start_data = date('Y-m-d', $one_year_ago_timestamp);
            }
            //获取对用的图表坐标数据
            $chartInfo = Db::table($v['table'])->field($field)->select();

            $res_data = [];
            $datetime = [];
            //循环坐标数据,将相同字段的数据放在一起
            foreach ($chartInfo as $cv) {
                $datetime[] = $cv['datetime'];
                foreach ($fields as $fk => $fv) {
                    $res_data[$fk][] = $cv[$fk];
                }
            }
            $chartInfo_data = [];
            $y_axial_2_data = [];//双Y轴数据
            //循环相同的坐标数据,将每个坐标数据进行充重组成新的数据给前端
            foreach ($res_data as $rk => $rv) {
                if ($rk == $v['y_axial_2']){
                    $y_axial_2_data = [
                        'name' => $datetime,
                        'val' => $rv,
                        'line' => $fields[$rk],
                        'min_interval' => (float)$v['min_interval'],
                        'max_interval' => (float)$v['max_interval'],
                    ];
                }else{
                    $y_axial_2_data = [];
                }

                $chartInfo_data[] = [
                    'name' => $datetime,
                    'val' => $rv,
                    'line' => $fields[$rk],
                    'min_interval' => (float)$v['min_interval'],
                    'max_interval' => (float)$v['max_interval'],
                    'y_axial_2_data' => $y_axial_2_data,
                ];
            }

            //判断内容是否收费
            //检测用户是否有权访问内容
            $is_member_expire = 0;
            $hasMembershipExpiryRecord = $this->hasMembershipExpiryRecordArticles($user['id'], 'chart_parent', $v['id'], $v['parent_id']);
            if ($hasMembershipExpiryRecord['code'] != 200) {
                //用户没买分类
                $is_member_expire = 1;//会员是否到期|0:否,1:是
            }

            //内容数组
            $res[] = [
                'id' => $v['id'],
                'name' => $v['name'],
                'title' => $v['title'],
                'data' => $chartInfo_data,
//                'y_axial_2_data' => $y_axial_2_data,
                'membership_price' => $v['membership_price'],//会员价格|0:免费
                'is_member_expire' => $is_member_expire,//会员是否到期|0:否,1:是
            ];

        }
        $this->success('', $res);
    }


}
